import {fetch} from '@tauri-apps/plugin-http'

export interface MusicInfo {
    title: string;
    singer: string;
    album: string;
    quality: string;
    interval: string;
    size: string;
    cover: string;
    url: string;
    lrc: string;
}

export interface Music {
    code: number;
    msg: string;
    data: MusicInfo;
    reqtime: string;
}

export interface MusicSrc {
    song: string,
    singer: string,
    src: string,
    picture?: string,
    time: string
}

export interface MusicContent {
    copyright: string,
    copyrightId: string,
    copyrightType: string,
    img: string,
    txt: string,
    txt2: string,
    txt3: string,
    txt5: string,
    vip: string,
    qualities: string[],
    showTag: string[],
    mvCopyright: string,
    mvId: string,
    resId: string,
    resType: string,
    songId: string,
    songData: string
}

export async function getMusicSrc(copyrightId: string, songId: string, albumId: string) {
    const baseUrl = `http://app.c.nf.migu.cn/MIGUM2.0/strategy/listen-url/v2.4?
    &netType=00&resourceType=2&toneFlag=PQ&
    copyrightId=${copyrightId}&songId=${songId}&albumId=${albumId}`
    const response = await fetch(baseUrl, {method: 'GET', headers: {channel: "014000D"}})
    return (await response.json())
}

export class MusicListApi {
    private readonly baseUrl: string = ''
    // 榜单
    private readonly topId: string = ''
    constructor(baseUrl: string, topId: string) {
        this.baseUrl = baseUrl
        this.topId = topId
    }

    async getMusicList() {
        const response = await fetch(
            `${this.baseUrl}${this.topId}`, {
                method: 'GET',
                headers: {
                    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'
                }
            })
        const data = await response.json()
        return data.data.contents
    }
}
